package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.VO.*;
import com.company.project.model.VtZhusu;
import com.company.project.model.VtZuzhuang;
import com.company.project.service.VtManagerService;
import com.company.project.service.VtZuzhuangService;
import com.company.project.util.DingManager;
import com.company.project.util.ExcelUtils;
import com.company.project.util.IDingManager;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Objects;

/**
* Created by CodeGenerator on 2023/11/14.
*/
@RestController
@RequestMapping("/vt/zuzhuang")
public class VtZuzhuangController {
    @Resource
    private VtZuzhuangService vtZuzhuangService;
    @Resource
    private VtManagerService vtManagerService;
    @PostMapping("/add")
    public Result add(@RequestBody VtZuzhuang vtZuzhuang) {
        vtZuzhuang.setIsdel(0);
        vtZuzhuang.setIsexamine(0);
        System.out.println(vtZuzhuang);
        if (Objects.isNull(vtZuzhuang.getId()))
            vtZuzhuangService.save(vtZuzhuang);
        else
            vtZuzhuangService.update(vtZuzhuang);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        vtZuzhuangService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(VtZuzhuang vtZuzhuang) {
        vtZuzhuangService.update(vtZuzhuang);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        VtZuzhuang vtZuzhuang = vtZuzhuangService.findById(id);
        return ResultGenerator.genSuccessResult(vtZuzhuang);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,@RequestHeader("token") String token) {
        UserVO userVO = vtManagerService.findUserByToken(token);
        System.out.println(userVO);
        VtZuzhuang vtZuzhuang = new VtZuzhuang();
        vtZuzhuang.setGroupid(userVO.getGroupId());
        vtZuzhuang.setIsdel(0);
        PageHelper.startPage(page, size);
        List<ZuzhuangVO> list = vtZuzhuangService.findListByGroupId(vtZuzhuang);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/todayList")
    public Result todayList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,@RequestHeader("token") String token) {
        UserVO userVO = vtManagerService.findUserByToken(token);
        System.out.println(userVO);
        LocalDate currentDate = LocalDate.now();
        // 设置当天的开始时间
        LocalDateTime startOfDay = currentDate.atStartOfDay();
        long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
        // 设置当天的结束时间
        LocalTime endOfDay = LocalTime.MAX;
        LocalDateTime endOfDate = LocalDateTime.of(currentDate, endOfDay);
        long endTimestamp = endOfDate.toEpochSecond(ZoneOffset.UTC);
        // 输出结果
        System.out.println("当天的开始时间戳：" + startTimestamp);
        System.out.println("当天的结束时间戳：" + endTimestamp);
        PageHelper.startPage(page, size);
        List<ZuzhuangVO> list =vtZuzhuangService.findTodayAllList(startTimestamp,endTimestamp,0);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    @PostMapping("/historyList")
    public Result historyList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,@RequestHeader("token") String token, @RequestBody FilterVO filterVO) {
        // 设置当天的开始时间
        PageHelper.startPage(page, size);
        List<ZuzhuangVO> list = vtZuzhuangService.findHistoryAllList(1,filterVO);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/todayAllList")
    public Result todayAllList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,@RequestHeader("token") String token) {
        UserVO userVO = vtManagerService.findUserByToken(token);
        System.out.println(userVO);
        if(userVO.getRoleId() != 6) {
            ResultGenerator.genFailResult("暂无权限");
        }
        LocalDate currentDate = LocalDate.now();
        // 设置当天的开始时间
        LocalDateTime startOfDay = currentDate.atStartOfDay();
        long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
        // 设置当天的结束时间
        LocalTime endOfDay = LocalTime.MAX;
        LocalDateTime endOfDate = LocalDateTime.of(currentDate, endOfDay);
        long endTimestamp = endOfDate.toEpochSecond(ZoneOffset.UTC);
        // 输出结果
        System.out.println("当天的开始时间戳：" + startTimestamp);
        System.out.println("当天的结束时间戳：" + endTimestamp);
        PageHelper.startPage(page, size);
        List<ZuzhuangVO> list =vtZuzhuangService.findTodayAllList(startTimestamp,endTimestamp, 2);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    @PostMapping("/historyAllList")
    public Result historyAllList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size, @RequestHeader("token") String token, @RequestBody FilterVO filterVO) {
        UserVO userVO = vtManagerService.findUserByToken(token);
        if(userVO.getRoleId() != 3) {
            ResultGenerator.genFailResult("暂无权限");
        }

        PageHelper.startPage(page, size);
        List<ZuzhuangVO> list =vtZuzhuangService.findHistoryAllList(3, filterVO);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @PostMapping("/approveForm")
    public Result approveForm(@RequestBody VtZuzhuang vtZuzhuang) {
        vtZuzhuangService.update(vtZuzhuang);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/getStatisticsList")
    public Result getStatisticsList() {
        List<StatisticsListVO> statisticsListVOList = vtZuzhuangService.getStatisticsList();
        return ResultGenerator.genSuccessResult(statisticsListVOList);
    }

    @GetMapping("/getStatisticsDetail")
    public Result getStatisticsDetail(@RequestParam Integer groupId,@RequestParam Integer classes) {
        List<StatisticsDetailDO> statisticsDetailDOList = vtZuzhuangService.getStatisticsDetail(groupId,classes);
        return ResultGenerator.genSuccessResult(statisticsDetailDOList);
    }


    @GetMapping("/export")
    public void export(@RequestParam Integer groupId, @RequestParam Integer classes, HttpServletResponse response) throws Exception {
//        IDingManager iDingManager = new DingManager();
//        String result = iDingManager.sendDingMsg("13552013783","测试");
        List<StatisticsDetailDO> statisticsDetailDOList = vtZuzhuangService.getStatisticsDetail(groupId,classes);
        ExcelUtils.downloadExcel(response, "生产日报表", "sheet1", statisticsDetailDOList, StatisticsDetailDO.class);

    }
    @GetMapping("/getExportLink")
    public Result getExportLink(@RequestParam Integer groupId, @RequestParam Integer classes, @RequestParam String text) throws Exception {
//        Properties properties = new Properties();
//        properties.load(new FileReader(".\\src\\main\\resources\\config.properties"));
        // 读取配置项
        String address = "http://120.211.16.31:8082";
        String url = address + "/vt/zuzhuang/export?groupId="+groupId +"&classes="+classes;
        IDingManager iDingManager = new DingManager();
        String result = iDingManager.sendDingLink("13552013783",url,"注塑车间生产日报表",text);
//        List<StatisticsDetailDO> statisticsDetailDOList = vtZhusuService.getStatisticsDetail(groupId,classes);
//        ExcelUtils.downloadExcel(response, "文件名", "sheet1", statisticsDetailDOList, StatisticsDetailDO.class);
        return ResultGenerator.genSuccessResult(result);
    }
}
